<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<!--edge浏览器H5兼容设置-->
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<!--360浏览器H5兼容设置-->
		<meta name="renderer" content="webkit" />
		<title>电脑商城</title>
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<!--导入核心文件-->
		<script src="../bootstrap3/js/holder.js"></script>
		<link href="../bootstrap3/css/bootstrap.css" rel="stylesheet" type="text/css">
		<script src="../bootstrap3/jquery-1.9.1.min.js"></script>
		<script src="../bootstrap3/js/bootstrap.js"></script>
		<script src="../bootstrap3/js/jquery.cookie.js" type="text/javascript" charset="utf-8"></script>

		<!-- 字体图标 -->
		<link rel="stylesheet" href="../bootstrap3/font-awesome-4.7.0/css/font-awesome.css" />
		<link rel="stylesheet" type="text/css" href="../css/layout.css" />
		<link rel="stylesheet" type="text/css" href="../css/top.css" />
		<link rel="stylesheet" type="text/css" href="../css/footer.css" />
	</head>

	<body>
		<!--头部-->
		<header class="header">
			<!--电脑商城logo-->
			<div class="row">
				<div class="col-md-3">
					<a href="index_business.html">
						<img src="../images/index/stumalllogo.png" />
					</a>
				</div>
				<!--快捷选项-->
				<div class="col-md-9 top-item">
					<ul class="list-inline pull-right">
						<li><a href="orders.html"><span class="fa fa-file-text"></span>&nbsp;订单</a></li>
						<li class="li-split">|</li>
						<li><a href="cart.html"><span class="fa fa-cart-plus"></span>&nbsp;购物车</a></li>
						<li class="li-split">|</li>
						<li>
							<!--下列列表按钮 ：管理-->
							<div class="btn-group">
								<button type="button" class="btn btn-link dropdown-toggle" data-toggle="dropdown">
									<span id="top-dropdown-btn"><span class="fa fa-gears"></span>&nbsp;管理 <span class="caret"></span></span>
								</button>
								<ul class="dropdown-menu top-dropdown-ul" role="menu">
									<li><a href="password_business.html">修改密码</a></li>
									<li><a href="userdata_business.html">个人资料</a></li>
									<li><a href="upload_business.html">上传营业执照</a></li>
									<li><a href="address_business.html">收货管理	</a></li>
								</ul>
							</div>
						</li>
						<li class="li-split">|</li>
						<li><a href="login.html"><span class="fa fa-user"></span>&nbsp;登录</a></li>
					</ul>
				</div>
			</div>
		</header>
		<!--导航 -->
		<!--分割导航和顶部-->
		<div class="row top-nav">
			<div class="col-md-6">
				<ul class="nav nav-pills">
					<li>
						<a href="#"></a>
					</li>
					<li class="active"><a href="index_business.html"><span class="fa fa-home"></span></a></li>
					<form id="form-search" action="search_business.html" class="form-inline pull-right" role="form">
						<div class="form-group" style="float: left">
							<input type="text" class="form-control" id="search" name="title" placeholder="请输入商品名称">
						</div>
						<input id="btn-search" type="submit" class="btn btn-primary pull-right" style="float: left" value="搜索" />
					</form>
				</ul>
			</div>
		</div>
		<!--头部结束-->
		<!--导航结束-->
		<div class="container">
			<div class="col-md-2">
				<!--左侧导航开始-->
				<div class="panel-group" id="accordion">
					<div class="panel panel-default">
						<div class="panel-heading">
							<!--主选项：我的订单-->
							<p class="panel-title">
								<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">我的订单</a>
							</p>
						</div>
						<div id="collapseOne" class="panel-collapse collapse">
							<div class="panel-body">
								<div><a href="orders.html">全部订单</a></div>
							</div>
						</div>
					</div>
					<div class="panel panel-default">
						<div class="panel-heading">
							<!--主选项：资料修改-->
							<p class="panel-title">
								<a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">资料修改</a>
							</p>
						</div>
						<div id="collapseTwo" class="panel-collapse collapse in">
							<div class="panel-body">
								<div><a href="password_business.html">修改密码</a></div>
								<div><a href="userdata_business.html">个人资料</a></div>
								<div><a href="upload_business.html"><b>上传营业执照</b></a></div>
								<div><a href="address_business.html">收货管理</a></div>
							</div>
						</div>
					</div>
				</div>
				<!--左侧导航结束-->
			</div>
			<div class="col-md-10	">
				<div class="panel panel-default">
					<ul class="nav nav-tabs">
						<li><a href="password_business.html">修改密码</a></li>
						<li><a href="userdata_business.html">个人资料</a></li>
						<li class="active"><a href="#">上传营业执照</a></li>
						<li><a href="address_business.html">收货管理</a></li>
					</ul>
					<div class="panel-body">
						<!--上传头像表单开始-->
						<form id="form-change-avatar" class="form-horizontal" role="form">
							<div class="form-group">
								<label class="col-md-2 control-label">上传营业执照:</label>
								<div class="col-md-5">
									<img id="img-avatar" src="../images/index/user.jpg" class="img-responsive" />
								</div>
								<div class="clearfix"></div>
								<div class="col-md-offset-2 col-md-4">
							<!---->		<input type="file" name="file">
								</div>
							</div>
							<div class="form-group">
								<div class="col-sm-offset-2 col-sm-10">
							<!---->		<input id="btn-change-avatar" type="button" class="btn btn-primary" value="上传" />
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
		<script type="text/javascript">
			/**
			 * 一旦检测到当前的页面被加载就会出发ready方法 ready方法里放置ajax
			 * $(document).ready(function(){
			 *     //编写业务代码
			 * });
			 */
			$(document).ready(function () {
				let avatar=$.cookie("avatar");//用avatar接收cookie中的头像的值
				console.log(avatar);

				//将cookie值后期处理设置到头像的src属性中
				$("#img-avatar").attr("src",$.cookie("avatar"));
			})

			/*
                1.给按钮添加监听是否被点击 调用click方法自动完成监听click方法要求传递一个function函数
                    function函数里调用ajax函数，ajax函数传递方法体方法体要求为一段大括号，再讲参数列表显示的结构写出来
                    data可调用序列化自动完成序列化某一个表单 用id选择器选择form表单，再去序列化一个key value键值对
            */
			$("#btn-change-avatar").click(function () {
				$.ajax({
					url:"/user/change_avatar",
					type:"POST",
					data:new FormData($("#form-change-avatar")[0]),//serialize()无法解析文件类型FormData类：将表单当中的数据保持原有的数据结构进行数据的提交
					processData:false,//表示处理数据的形式，false表示关闭处理数据
					contentType:false,//表示提交数据的形式，关闭默认提交数据的形式
					dataType:"JSON",
					success:function (json) {
						/*alert(json.state)*/
						if(json.state==200){
							alert("头像修改成功");
							//将服务器端返回的头像地址设置img标签的src属性上
							//attr（属性，属性值）:给某个属性设置某个值
							$("#img-avatar").attr("src",json.data)//给一个已存在的属性去设置一个新的值
							//将服务器端返回的头像设置到Cookie中
							$.cookie("avatar",json.data,{expires:7});////time单位天
						}else {
							alert("头像修改失败")
						}
					},
					error:function (xhr) {
						alert("修改头像时产生未知的异常"+xhr.message);//自动产生错误描述信息
					}
				})
			})
		</script>
	</body>

</html>